﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class RepairReportEdit : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(RepairReportEdit));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        //public string sFileName = "appfile";

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            string sModelName = "";
            DataSet ds = null;
            //divAudi.Visible = false;
            try
            {
                buy = new BuyDevice();
                if (m_Id == 0)
                {
                    if (!checkModelRightById(183, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                    DataTable dt = buy.getUserDept(iUserId);
                    //txtDeptId.Text = Convert.ToString(dt.Rows[0]["deptid"]);
                    //txtDeptName.Text = Convert.ToString(dt.Rows[0]["deptname"]);                    
                }
                else
                {
                    //if (Convert.ToInt32(Request["audi"]) == 1)
                    //{
                    //    if (!checkModelRightById(185, ref sModelName, (clsBaseLib)buy))
                    //    {
                    //        errorUrl(sModelName);
                    //        return;
                    //    }
                    //    trbtn.Visible = false;
                    //    //divAudi.Visible = true;
                    //}
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(182, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    
                    else
                    {
                        if (!checkModelRightById(185, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    //ds = buy.getAppInfoById(m_Id);
                    ds = deviceinfo.getRepairReportInfoByPlanId(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        hidPortType.Value = m_PortType;
                        txtPlanCode.Enabled = false;
                        txtDevID.Value = Convert.ToString(dt.Rows[0]["DevID"]);
                        txtPlanID.Value = Convert.ToString(dt.Rows[0]["PlanID"]);
                        txtPlanCode.Text = Convert.ToString(dt.Rows[0]["PlanCode"]);
                        if (dt.Rows[0]["PlanCode"].ToString().Trim() == "")
                        {
                            txtPlanCode.Enabled = true;
                        }
                        txtPrice.Text = dt.Rows[0]["ActualFeiy"].ToString().Trim();
                        lbDevCode.Text = Convert.ToString(dt.Rows[0]["DeviceCode"]);
                        lbDevName.Text = Convert.ToString(dt.Rows[0]["DeviceName"]);
                        lbUesDept.Text = Convert.ToString(dt.Rows[0]["DeptName"]);
                        lbDevSpec.Text = Convert.ToString(dt.Rows[0]["Devicespec"]);
                        if (dt.Rows[0]["dBeginTime"] != null && dt.Rows[0]["dBeginTime"].ToString().Trim() != "")
                        {
                            txtBeginDate.Text = Convert.ToDateTime(dt.Rows[0]["dBeginTime"]).ToString("yyyy-MM-dd");
                        }
                        if (dt.Rows[0]["dEndTime"] != null && dt.Rows[0]["dEndTime"].ToString().Trim() != "")
                        {
                            txtEndDate.Text = Convert.ToDateTime(dt.Rows[0]["dEndTime"]).ToString("yyyy-MM-dd");
                        }
                        txtNeed.Text = Convert.ToString(dt.Rows[0]["ShishiComment"]);

                        txtDevXingneng.Text = Convert.ToString(dt.Rows[0]["DevXingneng"]);
                        txtProductPinzhi.Text = Convert.ToString(dt.Rows[0]["ProductPinzhi"]);
                        txtComment.Text = Convert.ToString(dt.Rows[0]["Comment"]);

                    }
                    DataSet dsItem = new DataSet();
                    dsItem = deviceinfo.getItemOrPartInfoByPlanId(Convert.ToInt32(dt.Rows[0]["PlanID"]), m_PortType);
                    if (dsItem.Tables.Count > 0)
                    {
                        #region //获取维修计划维修项目列表
                        dt = dsItem.Tables[0];
                        int iRowCount = dt.Rows.Count;
                        StringBuilder sbu = new StringBuilder();
                        for (int i = 0; i < iRowCount; i++)
                        {
                            sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removeItem(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                    </tr>", dt.Rows[i]["ItemName"], dt.Rows[i]["ID"], dt.Rows[i]["ItemNeed"], dt.Rows[i]["comment"]));
                        }
                        ltrPlanItem.Text = Convert.ToString(sbu);
                        #endregion

                        if (dsItem.Tables.Count > 1)
                        {
                            #region //获取维修设备备件列表
                            dt = dsItem.Tables[1];
                            rptPart.DataSource = dt;
                            rptPart.DataBind();
                            #endregion
                        }
                    }
                }
            }
//            else
//            {
//                //lbAppDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
//                //lbUserName.Text = sUserName;
//                ltrPlanItem.Text = @"<tr>
//                        <td align=""left"" width=""120"">
//                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removeItem(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" /><input style=""width: 64px"" type=""hidden"" value=""0"" /></td>
//                        <td width=""70"">
//                            <input  style=""width: 70px"" type=""text"" /></td>
//                        <td width=""70"">
//                            <input  style=""width: 70px"" type=""text"" /></td>
//                        <td width=""70"">
//                            <input  style=""width: 70px"" type=""text"" /></td>
//                    </tr>";
//            }

        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }
        string m_PortType
        {
            get
            {
                return Request["portType"];
            }
        }

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int id, string dcode, string BeginDate, string EndDate, string Need, string[] Items, string[] parts, string price, string DevXingneng, string ProductPinzhi, string comment, string porttype1)
        {
            //aid,dcode,BeginDate,EndDate,Need,Items,parts,
//price,DevXingneng,ProductPinzhi,comment,porttype1
            //BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            bool bResult = false;
            try
            {
                //buy = new BuyDevice();
                dcode = dcode.Replace("'", "''");
                //sReason = sReason.Replace("'", "''");
                Need = Need.Replace("'", "''");
                for (int i = 0; i < Items.Length; i++)
                {
                    Items[i] = Items[i].Replace("'", "''");
                }
                for (int i = 0; i < parts.Length; i++)
                {
                    parts[i] = parts[i].Replace("'", "''");
                }
                //float fPrice = Convert.tod
                bResult = deviceinfo.setRepairReportInfo(id, dcode, BeginDate, EndDate, Need, Items, parts, price, DevXingneng, ProductPinzhi, comment, porttype1);
            }
            catch(Exception e)
            {
                Response.Write("<script languagwe='javascript'>alert('"+e.Message+"');</script>");
                bResult = false;
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            return bResult;
        }

        #endregion

        #region 获得备件信息

        [Ajax.AjaxMethod]
        public string getPartInfo11(int iTypeId)
        {
            //BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            DataTable dt = null;
            try
            {
                //buy = new BuyDevice();
                dt = deviceinfo.getDevPartInfoById(iTypeId).Tables[0];
            }
            catch (Exception e)
            {
                return Convert.ToString(e);
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            #region //获取维修设备备件列表
            int iRowCount = dt.Rows.Count;
            StringBuilder sbu = new StringBuilder();
            for (int i = 0; i < iRowCount; i++)
            {
                sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removeItem(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{4}"" /></td>
                    </tr>", dt.Rows[i]["PartCode"], dt.Rows[i]["PartID"], dt.Rows[i]["PartName"], dt.Rows[i]["Xinghaogg"], dt.Rows[i]["iNeedNum"]));
            }
            //ltrPlanPart.Text = Convert.ToString(sbu);
            #endregion
            return Convert.ToString(sbu);
        }

        [Ajax.AjaxMethod]
        public DataTable getPartInfo(string sIds)
        {
            DeviceInfo deviceinfo = new DeviceInfo();
            DataTable dt = null;
            try
            {
                dt = deviceinfo.getResultDevPartInfo(sIds);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            return dt;
        }

        #endregion

        #region 审核申请

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool audiApp(int iAppId, int iState, string sFile, string sMemo)
        {
            BuyDevice buy = null;
            bool bResult = false;
            try
            {
                buy = new BuyDevice();
                sFile = sFile.Replace("'", "''");
                sMemo = sMemo.Replace("'", "''");
                bResult = buy.audiAppInfoById(iAppId, iUserId, sFile, sMemo, iState);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion
}
}